define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    //del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('id'), sortable: true},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {
                            field: 'score',
                            title: __('Score'),
                            sortable: true,
                            search: false
                        },
                        {
                            field: 'avatar',
                            title: __('Avatar'),
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image,
                            operate: false
                        },
                        {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {
                            field: 'profession.name',
                            title: __('学习专业'),
                            operate: 'LIKE',
                            search: false,
                            //formatter: Table.api.formatter.flag
                        },
                        {
                            field: 'tag',
                            title: __('会员标签'),
                            operate: 'LIKE',
                            //search: false,
                            formatter: Table.api.formatter.flag
                        },
                        {
                            field: 'create_time',
                            title: __('注册时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'update_time',
                            title: __('登录时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'extend.number_of_answers',
                            title: __('答题数量'),
                            operate: 'LIKE',
                            searchable: false
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            formatter: Table.api.formatter.status,
                            searchList: {1: __('Normal'), 2: __('Hidden')}
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: '开通会员',
                                    title: '开通会员',
                                    icon: 'fa fa-chevron-circle-right',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: function (row) {
                                        return 'user/user/recharge/type/1/ids/'+row.id;
                                    },
                                    success: function () {
                                        $(".btn-refresh").trigger("click");
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: '开通课程',
                                    title: '开通课程',
                                    icon: 'fa fa-chevron-circle-right',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: function (row) {
                                        return 'user/user/recharge/type/2/ids/'+row.id;
                                    },
                                    success: function () {
                                        $(".btn-refresh").trigger("click");
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: '查看开通课程',
                                    title: '查看开通课程',
                                    icon: 'fa fa-book',
                                    classname: 'btn btn-xs btn-primary btn-addtabs',
                                    url: function (row) {
                                        return 'user/course/index/uid/'+row.id;
                                    },
                                    success: function () {
                                        $(".btn-refresh").trigger("click");
                                    }
                                },
                                {
                                    name: 'detail',
                                    text: '查看开通专业',
                                    title: '查看开通专业',
                                    icon: 'fa fa-graduation-cap',
                                    classname: 'btn btn-xs btn-primary btn-addtabs',
                                    url: function (row) {
                                        return 'user/vip/index/uid/'+row.id;
                                    },
                                    success: function () {
                                        $(".btn-refresh").trigger("click");
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            $('.btn-batchRechargeVip').click(function (){
                var url = 'user/user/batchRechargeVip';
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('批量开通会员'), $(this).data() || {}, {
                    success: function (data, ret) {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        recharge: function () {
            Controller.api.bindevent();
        },
        batchrechargevip: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});